/**
 *  [Description]
 * @className:SeqnumBuilder
 * @version:v1.0.0 
 * @author:guozj<br>
 * 
 * Modification History:<br>
 * Date         Author      Version     Description <br>
 * -------------------------------------------------<br>
 * 2014-6-19     guozj       v1.0.0        create
 *
 *
 */
package com.warm.pump.frame.util;


/**
 *  [定长序列号生成器]
 * @className:SeqnumBuilder
 * @version:v1.0.0 
 * @author:guozj<br>
 * 
 * Modification History:<br>
 * Date         Author      Version     Description <br>
 * -------------------------------------------------<br>
 * 2014-6-19     guozj       v1.0.0        create
 *
 *
 */
public class SeqnumBuilder {
	
    private static volatile long seqnum = 0L;
    
    public synchronized static String getFixedSeqnum(int length)
    {
        seqnum = seqnum >= Long.MAX_VALUE ? 0 : seqnum + 1;
        
        String strSeqnum = String.valueOf(seqnum);
        int strSeqnumLen = strSeqnum.length();
        if (strSeqnumLen > length)
        {
            return strSeqnum.substring(strSeqnumLen - length);
        }
        else
        {
            // 将seqnum转换为字符串，长度小于六位的场合左补0
            return String.format("%0" + length + "d", seqnum);
        }
    }
}
